package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;


import io.netty.channel.*;
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettySocketChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportServerProxyClientTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;


@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleSocksReportServerProxyClientTypeAdvanced
        extends AbstractHandleSocksReportServerProxyClientTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        NettySocketChannelContext nettySocketChannelContext = (NettySocketChannelContext) nettyChannelContext;
        Channel visitorrChannel = nettySocketChannelContext.channel();
        String host = nettyProxyMsg.getTargetIpString();
        Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());

        byte[] visitorId = nettyProxyMsg.getVisitorId();
        byte[] targetClientId = nettyProxyMsg.getClientId();
        Socks5AddressType socks5AddressType = nettySocketChannelContext.getSocks5AddressType();
        byte socks5AddressTypeByte = socks5AddressType.byteValue();

        String msgVisitorId = new String(visitorId);
        ChannelAttributeKeyUtils.buildVisitorId(visitorrChannel, msgVisitorId);
        NettyTransferChannelContext.pushVisitor(visitorrChannel, msgVisitorId);
        ChannelAttributeKeyUtils.buildSocks5AddressType(visitorrChannel,socks5AddressTypeByte);

        // 客户端连接客户端传输通道
        Channel loadBalance = ChannelContext.getLoadBalance(targetClientId);
        if(ObjectUtils.isNotEmpty(loadBalance)) {
            // 下发创建新链接指令
            NettyProxyMsg otherClientConnectServer = new NettyProxyMsg();
            // data ---》Socks5AddressType
            otherClientConnectServer.setClientTargetIp(host);
            otherClientConnectServer.setClientTargetPort(port);
            otherClientConnectServer.setVisitorId(visitorId);
            otherClientConnectServer.setClientId(targetClientId);
            otherClientConnectServer.setType(ProxyMessageType.SOCKS_DISTRIBUTE_SERVER_PROXY_CLIENT_CONNECTION_INIT_);
            loadBalance.writeAndFlush(otherClientConnectServer);
        }else {
            log.error("服务端代理客户端遇到错误，无法通过客户端ID获取当前客户端通道");
        }
    }

}
